package base;

import consumer.ConsumerHandler;
import consumer.handler.NormalHandler;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : zGame
 * @version V1.0
 * @Project: rokectmqprac
 * @Package base
 * @Description: 负责消息的派发  线程和handler派发,解包放到对应逻辑线程去解
 * @date Date : 2022年01月11日 14:24
 */
public class ConsumerMsgDispatcher implements  HandlerAutoRegister{
    
    private Map<String, ConsumerHandler> handlerMap = new HashMap<String, ConsumerHandler>();
    
    public void dispatch(MessageExt msg){
       
       // String handlerKey,String threadTag,String threadKey, byte[] body
    // 根据threadTag 和 threadkey 去找对应处理线程... 这里直接处理
        //handlerMap
        Map<String, String> prt = msg.getProperties();
        prt.get("handlerKey");
        prt.get("threadTag");
        prt.get("threadKey");
        msg.getBody();
        
    }
    
    @Override
    public void autoRegisterHandler() {
        //这里暂时手动注册,可以换为自动扫描包,进行注册
        handlerMap.put(NormalHandler.class.getSimpleName(),new NormalHandler());
    }
    
    private static class DEFAULT{
        private static final ConsumerMsgDispatcher provider = new ConsumerMsgDispatcher();
    }
    public static ConsumerMsgDispatcher getDefault(){
        return DEFAULT.provider;
    }
}
